<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\common\library\Page;

class ReportController extends AdminBaseController
{
    /**
     * 总入口
     * */
    public function winReport(){
        $type=$this->request->param('type','','string');
        $game_id=$this->request->param('game_id','','string');
        if(in_array($type,['month','week','day'])){
            switch($type){
                case 'month':
                    $time='%Y-%m';
                    break;
                case 'week':
                    $time='%Y-%u';
                    break;
                case 'day':
                    $time='%Y-%m-%d';
                    break;
            }
        }else{
            return 'error';
        }
        $re=$this->getData($time,$game_id);
        $game_list=db('game')->field('id,game_name')->select();
        $this->assign([
            'game_list'=>$game_list,
            'list'=>$re['list'],
            'page'=>$re['page'],
            'type'=>$type
        ]);

        return $this->fetch();
    }
    /**
     * 查询获取数据方法
     * */
    private function getData($time,$game_id){
        $count=db('game_bet')
            ->field("DATE_FORMAT(updated_time,'".$time."') times,count(id) count,win_result,sum(win_gold) sum_gold,sum(bet_gold) sum_bet_gold")
            ->group("times,win_result")
            ->order("times desc");
        if($game_id != 0){
            $count=$count->where("game_id='$game_id'");
        }
        $count=$count->count();
        $list=db('game_bet')
            ->field("DATE_FORMAT(updated_time,'".$time."') times,count(id) count,win_result,sum(win_gold) sum_gold,sum(bet_gold) sum_bet_gold")
            ->group("times,win_result")
            ->order("times desc");
        if($game_id != 0){
            $list=$list->where("game_id='$game_id'");
        }
        $list=$list->paginate(20,$count)->appends($this->request->param());
        $page=$list->render();
        $list_re=[];
        if(!is_array($list)){$list=$list->toArray();}

        foreach ($list['data'] as $v){
            if(isset($list_re[$v['times']])){
                $v_re=$list_re[$v['times']];
            }else{
                $v_re=[
                    'times'=>($time == '%Y-%u')?$this->getWeekTime($v['times']):$v['times'],
                    'sum_bet_gold'=>0,
                    'win_count'=>0,
                    'lose_count'=>0,
                    'win_gold'=>0,
                    'lose_gold'=>0,
                    'count'=>0
                ];
            }
            $v_re['sum_bet_gold']+=$v['sum_bet_gold'];
            if($v['win_result'] == 0){
                $v_re['lose_count']+=$v['count'];
                $v_re['lose_gold']+=$v['sum_gold'];
            }else{
                $v_re['win_count']+=$v['count'];
                $v_re['win_gold']+=$v['sum_gold'];
            }
            $v_re['count']+=$v['count'];
            $list_re[$v['times']]=$v_re;
        }
        return ['list'=>$list_re,'page'=>$page];
    }
    /**
     * 返回某年第几周的时间段
     * */
    private function getWeekTime($time){
        $time_arr=explode('-',$time);
        $year=(int)$time_arr[0];
        $week=(int)$time_arr[1];
        if ($week < 10)
        {
            $week = '0' . $week; // 注意：一定要转为 2位数，否则计算出错
        }
        $timestamp= strtotime($year . 'W' . $week);
        $timeStart = date('Y-m-d',$timestamp);
        $timeFinal = date('Y-m-d',strtotime('+1 week -1 second',$timestamp));
        return $timeStart.' - '.$timeFinal.'  （'.$year.'年第'.$week.'周）';
    }
    /**
     * 所有注单记录
     * */
    public function betlist(){
        $game_id=$this->request->param('game_id',0,'int');
        $list=db('game_bet')
            ->alias('g')
            ->join('user u','g.user_id=u.id')
            ->join('game','g.game_id=game.id')
            ->field('g.id,u.user_nickname user,game.game_name game,g.bet_gold,
            g.win_result,g.updated_time,g.site_id,g.win_gold')
            ->order('updated_time desc');
        if($game_id != 0){
            $list=$list->where("g.game_id='$game_id'");
        }
        $list=$list->paginate(20)->appends($this->request->param());
        $page=$list->render();
        $game_list=db('game')->field('id,game_name')->select();
        $this->assign([
            'game_list'=>$game_list,
            'list'=>$list,
            'page'=>$page
        ]);

        return $this->fetch();
    }
}